package com.ivan.toolkit.common;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Optional;

public final class Expirable<T> {
	
	
	public static <T> Expirable<T> at(T value, long expire, ChronoUnit unit) {
		return new Expirable<T>(value, expire, unit);
	}

	private Instant deadline;
	
	private T value;

	private Expirable(T value, long expire, ChronoUnit unit) {
		super();
		this.deadline = Instant.now().plus(expire, unit);
		this.value = value;
	}
		
	public boolean expired() {
		return Instant.now().isAfter(deadline);
	}

	public Optional<T> get() {
		return expired() ? Optional.empty() : Optional.of(value);
	}
	
}